package leetcode.tree;

/**
 * @author : zx
 * @version V1.0
 */
public class QiuErChaShuShenDu {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }


    private static int depth = 1;
    private static int max = 0;

    private static int dfs(TreeNode root){
        if (root == null){
            return 0;
        }
        if (depth > max){
            max = depth;
        }
        if (root.left != null){
            depth++;
            dfs(root.left);
            depth--;
        }
        if (root.right != null){
            depth++;
            dfs(root.right);
            depth--;
        }
        return max;
    }

    public int maxDepth(TreeNode root) {
        depth = 1;
        max = 0;
        return dfs(root);
    }
}
